//--------------在线下单 进口
//--------------添加随附单证
var attaSerIdIndex = 0;
var atta_edit_serid = "";

function addOrUpdateAtta() {
    if (atta_edit_serid != null && atta_edit_serid != "") {
        updateAttaTr(atta_edit_serid);
    } else {
        addAtta();
    }
}

function setattaSerIdIndex(value) {
    attaSerIdIndex = value;
}

function addAtta() {
    var atta_dzdm = $("#atta_dzdm").val();
    var atta_dzdmName = $("#atta_dzdmName").val();
    var atta_dzbm = $("#atta_dzbm").val();
    if (atta_dzdm == "") {
        //   alert("随附单证代码不能为空！");
        $.messager.alert('提示', "随附单证代码不能为空！");
        focusDemo($("#attaSelect"));
        return;
    } else if (atta_dzbm == "") {
        //   alert("随附单证编号不能为空！");
        $.messager.alert('提示', "随附单证编号不能为空！");
        $("#atta_dzbm").focus();
        return;
    }
    var addHtml = getNewAttaHtml(atta_dzdm, atta_dzdmName, atta_dzbm);
    attaSerIdIndex++;

    $("#atta_lastTR").before(addHtml);

    clearHgSfdjInput();

    focusDemo($("#attaSelect"));
    afterHgSfdjChange();
}

function editAttaTr(trSerNo) {
    atta_edit_serid = "" + trSerNo;

    var value = $("#atta_dzdm_" + trSerNo).val();
//     initSelectValueS($("#attaSelect"),value);
    setSelectValue($("#attaSelect"), "atta_dzdm", "atta_dzdmName",
                   "atta_dzdm" + "_" + trSerNo,
                   "atta_dzdmName" + "_" + trSerNo);
    editTr("atta_TR" + trSerNo);
    $("#atta_addButton").val("修改");
    focusDemo($("#attaSelect"));

}
function clearHgSfdjInput() {
    $("#atta_input_table input[type!='button']").each(
        function (index, element) {
            if ($(element).attr("selectData") && $(element).attr(
                    "isInit") && $(element).attr("isInit")
                                 == 1) {
                $(element).combogrid("clear");
            } else {
                $(element).val("");
            }
        });
    $("#atta_input_table select").each(
        function (index, element) {
            $(element).val("");
        });
}

function updateAttaTr() {
    var atta_dzdm = $("#atta_dzdm").val();
    var atta_dzbm = $("#atta_dzbm").val();

    if (atta_dzdm == "") {
        //    alert("随附单证代码不能为空！");
        $.messager.alert('提示', "随附单证代码不能为空！");
        focusDemo($("#attaSelect"));
        return;
    } else if (atta_dzbm == "") {
        //    alert("随附单证编号不能为空！");
        $.messager.alert('提示', "随附单证编号不能为空！");
        $("#atta_dzbm").focus();
        return;
    }

    updateTr("atta_input_table", atta_edit_serid);
    clearHgSfdjInput();

    focusDemo($("#attaSelect"));
    initAttaInput();
    afterHgSfdjChange();
}

function initAttaInput() {
    atta_edit_serid = "";
    $("#atta_addButton").val("添加");
}

function getNewAttaHtml(atta_dzdm, atta_dzdmName, atta_dzbm) {
    var idPrefixName = "atta";
    var hgattaName = "baHgbaseinfo.hgatta[" + attaSerIdIndex + "]";
    var result = "<tr id='" + idPrefixName + "_TR" + attaSerIdIndex
                 + "' onclick='editAttaTr(" + attaSerIdIndex + ")'>";
    result += "<td>";
    result +=
        getInput(idPrefixName + "_dzdm_" + attaSerIdIndex,
                 hgattaName + ".dzdm", atta_dzdm, 1, "width:100%");
    result +=
        getInput(idPrefixName + "_dzdmName_" + attaSerIdIndex, "",
                 atta_dzdmName, 0, "width:100%");
    result += "</td><td>";
    result +=
        getInput(idPrefixName + "_dzbm_" + attaSerIdIndex,
                 hgattaName + ".dzbm", atta_dzbm, 0, "width:100%");
    result += "</td><td>";
    result +=
        "<img src='../image/delete.gif' onclick='delAttaTr(\""
        + idPrefixName + "_TR" + attaSerIdIndex + "\")'/></td>";
    result += "</tr>";
    return result;
}

function delAttaTr(trId) {
    $("#cons.id" + trId).remove();
    initAttaInput();
    $("#atta_dzdm").focus();
    afterHgSfdjChange();
    if (event.stopPropagation) {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }

}

function setHgAttaSerId() {
    var hgAttaStr = "";
    $(".hgAttaSerId").each(function () {
        var value = $(this).val();
        hgAttaStr += value + ",";
    });
    hgAttaStr = hgAttaStr.substring(0, hgAttaStr.length - 1);
    $("#hgatta").val(hgAttaStr);
}

function editAtta(boxSerIdIndex) {
    var box_jzxh = $("#hgBox_jzxh_" + boxSerIdIndex).val();
    var box_jzxgg = $("#hgBox_jzxgg_" + boxSerIdIndex).val();
    var box_jzxzz = $("#hgBox_jzxzz_" + boxSerIdIndex).val();

    $("#box_jzxh").val(box_jzxh);
    $("#box_jzxgg").val(box_jzxgg);
    $("#box_jzxzz").val(box_jzxzz);
    $("#box_jzxh").focus();
}

//用于关检联合界面.
function openhgSfdjDIV() {
    $('#hgSfdjDIV').dialog('open').dialog('setTitle', '海关随附单据');
    bindPanelCloseIgnoreBeforeunload();
}

function afterHgSfdjChange() {
    var result = "";

    $("#hgSfdjForm input[id^='atta_dzdm_']").each(function () {
        result += " " + $(this).attr("value");
    });
    $("#hgSFDJInput").attr("value", result);
}
afterHgSfdjChange();
